package com.gnerv.pabu.basic.core.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gnerv.pabu.basic.core.dto.EmailConfigDTO;
import com.gnerv.pabu.basic.core.dto.EmailConfigSaveDTO;
import com.gnerv.pabu.basic.core.dto.EmailConfigSearchDTO;
import com.gnerv.pabu.basic.core.entity.EmailConfig;
import com.gnerv.pabu.basic.core.mapper.EmailConfigMapper;
import com.gnerv.pabu.basic.core.service.IEmailConfigService;
import com.gnerv.pabu.basic.core.utils.UUIDUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

@Slf4j
@Service
public class EmailConfigServiceImpl extends ServiceImpl<EmailConfigMapper, EmailConfig> implements IEmailConfigService {

    @Resource
    EmailConfigMapper emailConfigMapper;

    @Override
    public boolean saveEmailConfig(EmailConfigSaveDTO emailConfigSaveDTO) {
        if (StringUtils.isEmpty(emailConfigSaveDTO.getUkId())) {
            this.createEmailConfig(emailConfigSaveDTO);
        } else {
            this.updateEmailConfig(emailConfigSaveDTO);
        }
        return true;
    }

    @Override
    public boolean createEmailConfig(EmailConfigSaveDTO emailConfigSaveDTO) {
        emailConfigSaveDTO.setUkId(UUIDUtils.getLower());
        emailConfigMapper.insert(emailConfigSaveDTO.convert());
        return true;
    }

    @Override
    public boolean deleteByEmailConfigId(String emailConfigId) {
        emailConfigMapper.deleteById(emailConfigId);
        return true;
    }

    @Override
    public boolean updateEmailConfig(EmailConfigSaveDTO emailConfigSaveDTO) {
        emailConfigMapper.updateById(emailConfigSaveDTO.convert());
        return true;
    }

    @Override
    public boolean updateEmailConfigStatus(String emailConfigId, Integer status) {
        emailConfigMapper.updateEmailConfigStatus(emailConfigId, status);
        return true;
    }

    @Override
    public EmailConfigDTO selectEmailConfig(String emailConfigId) {
        EmailConfigDTO emailConfig = emailConfigMapper.selectEmailConfig(emailConfigId);
        return emailConfig;
    }

    @Override
    public Page<EmailConfigDTO> listEmailConfig(EmailConfigSearchDTO emailConfig, Long current, Long size) {
        Page<EmailConfigDTO> emailConfigs = emailConfigMapper.listEmailConfig(new Page<EmailConfigDTO>(current, size), emailConfig);
        return emailConfigs;
    }
}
